package ink.lovejinhu.leetcode;

import java.util.LinkedList;

/**
 * @author jinhu
 * created 2021-07-07 10:04
 */
public class Solution_0225 {
    LinkedList<Integer> v1;
    LinkedList<Integer> v2;
    /**
     * 用队列实现栈
     */
    /**
     * Initialize your data structure here.
     */
    public Solution_0225() {
        v1 = new LinkedList<Integer>();
        v2 = new LinkedList<Integer>();
    }

    /**
     * Push element x onto stack.
     */
    public void push(int x) {
        if (v1.isEmpty()) {  //v1是空
            v2.offer(x);
        } else {
            v1.offer(x);
        }
    }

    /**
     * Removes the element on top of the stack and returns that element.
     */
    public int pop() {

        if (v1.isEmpty()) {  //v1是空

            int record = Integer.MIN_VALUE;
            while (!v2.isEmpty()) {
                record = v2.poll();
                if (v2.peek() != null) {
                    v1.offer(record);
                }
            }

            return record;

        } else {


            int record = Integer.MIN_VALUE;
            while (!v1.isEmpty()) {
                record = v1.poll();
                if (v1.peek() != null) {
                    v2.offer(record);
                }
            }
            return record;

        }
    }

    /**
     * Get the top element.
     */
    public int top() {

        if (v1.isEmpty()) {  //v1是空

            int record = Integer.MIN_VALUE;
            while (!v2.isEmpty()) {
                record = v2.poll();
                v1.offer(record);
            }

            return record;

        } else {


            int record = Integer.MIN_VALUE;
            while (!v1.isEmpty()) {
                record = v1.poll();
                v2.offer(record);

            }
            return record;

        }
    }

    /**
     * Returns whether the stack is empty.
     */
    public boolean empty() {
        if (v1.isEmpty() && v2.isEmpty())
            return true;
        else
            return false;
    }

//    public static void main(String[] args) {
//        Solution_0225 myStack = new Solution_0225();
//        myStack.push(1);
//        myStack.push(2);
//        myStack.top(); // 返回 2
//        myStack.pop(); // 返回 2
//        myStack.empty(); // 返回 False
//        LinkedList<Integer> integers = new LinkedList<>();
//        integers.offer(1);
//        integers.offer(2);
//        integers.offer(3);
//        System.out.println(integers.poll());
//        System.out.println(integers.poll());
//    }
}
